<?php
namespace app\teacher\controller;

use app\common\model\TeacherBase;
use app\exam\model\ExamScheduleManager;
use app\teacher\model\TeacherExamManager;

class Exam extends TeacherBase
{
    const MISS_ID = "缺少考试编号";
    const MISS_DATE = "缺少日期";
    const MISS_PLACE = "缺少地点";
    const MISS_TOTAL_MOUNT = "缺少总人数";

    const MISS_PARA = "缺少输入数据";

    const SUCCESS_DELETE = "成功删除场次";

    const SUCCESS_UPDATE = "更新成功";
    const ERROR_UPDATE = "更新失败";
    const ERROR_STATUS = "更新状态失败";
    private $teacherExamManager;

    public function __construct()
    {
        parent::__construct();
        $this->teacherExamManager = new TeacherExamManager();
    }

    /**
     *
     * @api {get} /teacher/exam/ 读取考试批次
     * @apiDescription 读取考试批次
     * @apiGroup teacher
     *
     * @apiParam {string} [examid] 场次
     * @apiParam {string} [date] 日期
     * @apiParam {string} [status] 0可报名 1不可报名 2结束
     * @apiParam {string} [place] 地点
     * @apiParam {string} [total_mount] 总人数
     * @apiParam {string} [page] 页数1 默认5条 0表示所有数据
     *
     *@apiParamExample {json} Request-Example:
     *     {
     *       "examid": 10,
     *       "date": "2018-09-08 13:29:21"，
     *       "status" : 0 or 1,
     *       "place":"10栋312",
     *       "total_mount":100
     *     }
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 返回报名场次.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": [{"返回报名场次"}]
     *     }
     */
    public function index()
    {
        $this->permissionLevel(parent::MANAGER_PERMISSION);

        $examid = $_GET["examid"];
        $date = $_GET["date"];
        $status = $_GET["status"];
        $place = $_GET["place"];
        $total_mount = $_GET["total_mount"];
        $page = $_GET["page"];

        if(isset($examid)){
            $examinfo["id"] = $examid;
        }
        if(isset($date)){
            $examinfo["date"] = $date;
        }
        if(isset($status)){
            $examinfo["status"] = $status;
        }
        if(isset($place)){
            $examinfo["place"] = $place;
        }
        if(isset($total_mount)){
            $examinfo["total_mount"] = $total_mount;
        }
        if(!isset($page)){
            $page = 0;
        }

//        var_dump($page);
        //if($examinfo == null)
        //   echoJson(0, self::MISS_PARA);
        $result = $this->teacherExamManager->getTeacherExamList($examinfo,$page);
        
        $count = $this->teacherExamManager->getTeacherExamListCount();
        echo json_encode(array("code" => 1, "msg" => $result, "count"=>$count));
        //echoJson(1, $result);
    }

    /**
     *
     * @api {get} /teacher/exam/create 创建考试批次
     * @apiDescription 创建考试批次
     * @apiGroup teacher
     *
     * @apiParam {string} id 考试批次
     * @apiParam {string} date 日期
     * @apiParam {string} place 地点
     * @apiParam {string} total_mount 总人数
     *
     *@apiParamExample {json} Request-Example:
     *     {
     *       "date": "2018-09-08 13:29:21"，
     *       "place":"10栋312",
     *       "total_mount":100
     *     }
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 返回报名场次.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": [{"返回报名场次"}]
     *     }
     */
    public function create()
    {
        $this->permissionLevel(parent::MANAGER_PERMISSION);

        $id = $_GET["id"];
        $date = $_GET["date"];
        $place = $_GET["place"];
        $total_mount = $_GET["total_mount"];

        if(empty($id)){
            echoJson(0,self::MISS_ID);
        }else if(empty($date)){
            echoJson(0,self::MISS_DATE);
        }else if(empty($place)){
            echoJson(0,self::MISS_PLACE);
        }else if(empty($total_mount)){
            echoJson(0,self::MISS_TOTAL_MOUNT);
        }

        $examInfo = array(
            "id" => $id,
            "date" => $date,
            "place" => $place,
            "status"=> ExamScheduleManager::ACCEPT_EXAM,
            "total_mount" => $total_mount,
            "current_mount" => 0
        );

        if($this->teacherExamManager->createTeacherExam($examInfo)) {
            echoJson(1, "创建成功");
        }
    }

    /**
     *
     * @api {put} /teacher/exam/:id 更新考试批次
     * @apiDescription 创建考试批次
     * @apiGroup teacher
     *
     * @apiParam {string} [examid] 场次
     * @apiParam {string} [date] 日期
     * @apiParam {string} [place] 地点
     * @apiParam {string} [total_mount] 总人数
     *
     *@apiParamExample {json} Request-Example:
     *     {
     *       "examid": 10,
     *       "date": "2018-09-08 13:29:21"，
     *       "place":"10栋312",
     *       "total_mount":100
     *     }
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 返回报名场次.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": [{"返回报名场次"}]
     *     }
     */
    public function update($id){
        $this->permissionLevel(parent::MANAGER_PERMISSION);

        $examid = $_GET["examid"];
        $date = $_GET["date"];
        $place = $_GET["place"];
        $status = $_GET["status"];
        $total_mount = $_GET["total_mount"];

        if(isset($examid)){
            $examinfo["id"] = $examid;
        }
        if(isset($date)){
            $examinfo["date"] = $date;
        }
        if(isset($place)){
            $examinfo["place"] = $place;
        }
        if(isset($total_mount)){
            $examinfo["total_mount"] = $total_mount;
        }
        if(isset($status)){
            $examinfo["status"] = $status;
            if($status>3 || $status<0){
                echoJson(0,self::ERROR_STATUS);
            }
        }

        if($examinfo == null)
            echoJson(0, self::MISS_PARA);

        if($this->teacherExamManager->updateTeacherExam($id,$examinfo)){
            echoJson(1,self::SUCCESS_UPDATE);
        }else{
            echoJson(0,self::ERROR_UPDATE);
        }
    }

    /**
     *
     * @api {get} /teacher/exam/:id 读取考场下载人员名单
     * @apiDescription 读取考场下载人员名单
     * @apiGroup teacher
     *
     * @apiParam {string} id 考试批次
     * @apiParam {string} [download] 0 读取名单 1 下载名单
     *
     */
    public function read($id){
        //echoJson(1,$this->teacherExamManager->getTeacherExamDetail($id));
        //$this->show($this->teacherExamManager->getTeacherExamDetail($id));
        $download = $_GET["download"];
        if(!isset($download)){
            $download = 0;
        }

        if($download == 0)
            echoJson(1, $this->teacherExamManager->getTeacherExamDetail($id));
        else if($download == 1)
            echoJson(1, $this->teacherExamManager->downloadTeacherExam($id));
    }

    /**
     *
     * @api {delete} /teacher/exam/:id 删除考试场次
     * @apiDescription 删除考试场次
     * @apiGroup teacher
     *
     * @apiParam {string} id 考试批次
     *
     *
     */
    public function delete($id){
        $this->permissionLevel(parent::MANAGER_PERMISSION);
        if($this->teacherExamManager->deleteTeacherExam($id)){
            echoJson(1,self::SUCCESS_DELETE);
        }
    }
}